{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 线性回归（Linear Regression）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random\n",
    "from matplotlib import pyplot as plt\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "单层神经网络作为感知机、线性回归算法、Logistic回归算法的父类\n",
    "\"\"\"\n",
    "\n",
    "class SingleLayerNeuralNetwork:\n",
    "    \"\"\"\n",
    "    单层神经网络\n",
    "    1、使用 SingleLayerNeuralNetwork(1000, 0) 来初始化单层神经网络\n",
    "    2、使用 Object.getW() 来获取分类面参数\n",
    "    3、使用 Object.getDimen() 来获取训练样本的维数\n",
    "    \"\"\"\n",
    "    # 最大迭代次数\n",
    "    iteration = 10000\n",
    "    # 随机种子，决定迭代起始样本\n",
    "    randomstate = 0\n",
    "    # 分类面参数 w_1--w_i\n",
    "    W = None\n",
    "    # 分类面参数 w_0\n",
    "    w_0 = None\n",
    "    # 训练样本维度\n",
    "    dimen = None\n",
    "    # 训练样本数据\n",
    "    Xtrain = None\n",
    "    # 训练样本标签\n",
    "    Ytrain = None\n",
    "    # 测试样本数据\n",
    "    Xtest = None\n",
    "    # 测试样本预测的结果\n",
    "    Ypred = None\n",
    "    # 训练样本上预测准确率\n",
    "    train_accuracy = None\n",
    "    # 世界迭代次数\n",
    "    actual_iteration = None\n",
    "    \n",
    "    def __init__(self, iteration=10000, randomstate=0):\n",
    "        '''\n",
    "        初始化参数 iteration 和 randomstate\n",
    "        iteration：  最大迭代次数\n",
    "        randomstate：随机种子，决定训练起始样本\n",
    "        '''\n",
    "        self.iteration = iteration\n",
    "        self.randomstate = randomstate\n",
    "        \n",
    "    def getW(self):\n",
    "        '''\n",
    "        获取分类面参数 w_0--w_i\n",
    "        @return：元组类型，例如 (w_0, W)\n",
    "        '''\n",
    "        return (self.w_0, self.W)\n",
    "\n",
    "    def getTrainResult(self):\n",
    "        '''\n",
    "        获取训练结果\n",
    "        @return：元组类型。\n",
    "                 第一个元素表示实际迭代次数；\n",
    "                 第二个元素表示训练样本上的准确率\n",
    "        '''\n",
    "        return (self.actual_iteration, self.train_accuracy)\n",
    "    \n",
    "    def fit(self, Xtrain, Ytrain):\n",
    "        '''\n",
    "        训练测试样本\n",
    "        Xtrain： 训练样本的数据，N * d 矩阵，第 i 行表示第 i 个数据\n",
    "        Ytrain： 训练样本的标签，N * 1 矩阵，第 i 行表示第 i 个标签\n",
    "        @return：(实际迭代的次数, 训练样本集上测试的准确率)\n",
    "        '''\n",
    "        return (self.actual_iteration, self.train_accuracy)\n",
    "    \n",
    "    def predict(self, Xtest):\n",
    "        '''\n",
    "        对测试样本进行预测\n",
    "        Xtest：  测试样本的数据，N * d 矩阵，第 i 行表示第 i 个数据\n",
    "        @return：测试样本的预测结果，N * 1 矩阵，第 i 行表示第 i 个标签\n",
    "        '''\n",
    "        return self.Ypred\n",
    "    \n",
    "    def drawTwoDimenPic(self, traindata=True, testdata=False, line=True, color=['r','b','y','g','k']):\n",
    "        '''\n",
    "        画出包含训练集、测试集和分类面的二维图片，只适用于训练集为二维的情况（暂定）\n",
    "        traindata：是否包含训练集数据\n",
    "        testdata： 是否包含测试集数据\n",
    "        line：     是否包含分类面\n",
    "        @return：  图像\n",
    "        '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "线性回归算法，基于单层神经网络\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "\n",
    "class LinearRegression(SingleLayerNeuralNetwork):\n",
    "    \"\"\"\n",
    "    线性回归算法\n",
    "    \"\"\"\n",
    "    \n",
    "    def fit(self, Xtrain, Ytrain):\n",
    "        '''\n",
    "        训练测试样本\n",
    "        Xtrain： 训练样本的数据，N * d 矩阵，第 i 行表示第 i 个数据\n",
    "        Ytrain： 训练样本的标签，N * 1 矩阵，第 i 行表示第 i 个标签\n",
    "        @return：(实际迭代的次数, 训练样本集上测试的准确率)\n",
    "        '''\n",
    "        \n",
    "        return super().fit(Xtrain, Ytrain)\n",
    "\n",
    "    def predict(self, Xtest):\n",
    "        '''\n",
    "        对测试样本进行预测\n",
    "        Xtest：  测试样本的数据，N * d 矩阵，第 i 行表示第 i 个数据\n",
    "        @return：测试样本的预测结果，N * 1 矩阵，第 i 行表示第 i 个标签\n",
    "        '''\n",
    "        return super().predict(Xtest)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "l = LinearRegression(100,1)\n",
    "print(l.randomstate)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
